package com.cruise.h5.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cruise.common.web.result.Result;
import com.cruise.h5.pojo.dto.*;
import com.cruise.h5.pojo.vo.GetPlayVideoInfoVO;
import com.cruise.h5.pojo.vo.VideoListMainPageVO;
import com.cruise.h5.pojo.vo.VideoListVO;
import com.cruise.h5.service.VideoService;
import com.cruise.h5.util.DownLoadUtils;
import com.cruise.h5.util.UploadFilesUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cruise
 * @since 2021-03-25
 */
@RestController
@RequestMapping("/h5/video")
@Api(tags ="视频接口")
public class VideoController {


    @Resource
    private VideoService videoService;

    @Value("${upload-url}")
    private String uploadUrl ;


    @ApiOperation("上传视频接口")
    @PostMapping("/uploadVideo")
    public Result uploadVideo(@RequestParam("file") MultipartFile file){
        Result uploading = UploadFilesUtil.uploading(file);
        return uploading;
    }

    /**
     * 包括自定义标签的保存
     * @param videoDTO
     * @return
     */
    @ApiOperation("视频保存")
    @PostMapping("/saveVideo")
    public Result saveVideo(@RequestBody VideoDTO videoDTO){
        Result saveVideo = videoService.saveVideo(videoDTO);
        return saveVideo;
    }

    @ApiOperation("获得分类列表")
    @PostMapping("/getVideoCategoryTree")
    public Result getVideoCategoryTree(){
        Result videoCategoryTree = videoService.getVideoCategoryTree();
        return videoCategoryTree;
    }


    /**
     * 返回自定义标签和推荐标签，自定义标签在前面。
     * @return
     */
    @ApiOperation("根据分类获得推荐标签")
    @PostMapping("/getLabelByCategory")
    public Result getLabelByCategory(String categoryId){
        Result label = videoService.getLabelByCategory(categoryId);
        return label;
    }

    /**
     * 修改封面。
     * @return
     */
    @ApiOperation("修改封面")
    @PostMapping("/saveCoverPic")
    public Result saveCoverPic(@RequestBody CoverPicDTO coverPicDTO){
        Result label = videoService.saveCoverPic(coverPicDTO);
        return label;
    }

    @ApiOperation("点赞视频/取消点赞视频")
    @PostMapping("/like")
    public Result like(@RequestBody LikeDTO likeDTO){
        Integer likeCount  = videoService.like(likeDTO);
        return Result.success(likeCount);
    }

    @ApiOperation("视频列表")
    @PostMapping("/videoList")
    public Result<IPage<VideoListVO>> videoList(@RequestBody VideoListDTO videoListDTO){
        return videoService.videoList(videoListDTO);
    }

    /**
     * 展示 视频封面 图片   filePath = 2021/07/11/dc892ea93490437cb9916e5b8ff31e6e.jpeg
     * @return
     */
    @GetMapping(value = "/showCoverPicIO")
    public void  showCoverPicIo(String filePath , HttpServletRequest request, HttpServletResponse response) throws Exception {
       DownLoadUtils.download(uploadUrl + filePath,"fengmian",request,response);
    }

    /**
     * 展示视频 断点续传  filePath = 2021/07/11/dc892ea93490437cb9916e5b8ff31e6e.jpeg
     * @return
     */
    @GetMapping(value = "/showVideoIO")
    public void  showVideoIO(String filePath , HttpServletRequest request, HttpServletResponse response) throws Exception {
        DownLoadUtils.downRangeFile(uploadUrl + filePath,"fengmian",request,response);
    }

    /**
     * 播放页的信息
     * @return
     */
    @GetMapping(value = "/getPlayVideoInfo")
    public Result<GetPlayVideoInfoVO> getPlayVideoInfo(String videoId) throws Exception {
        return videoService.getPlayVideoInfo(videoId);
    }

    @ApiOperation("主页视频列表")
    @PostMapping("/videoListMainPage")
    public Result<IPage<VideoListMainPageVO>> videoListMainPage(@RequestBody VideoListMainPageDTO videoListMainPageDTO){
        return videoService.videoListMainPage(videoListMainPageDTO);
    }


}

